const {BrowserWindow,ipcMain,screen} = require('electron');
const path = require('path');
let msgWindow = null;
let msgWindowDisplay = false;
let msgWindowShowAnimationTimer = null;
let msgWindowHideAnimationTimer = null;
let msgWindowHideTimer = null;
exports.createMsgWindow = async function () {
    let screenInfo = screen.getPrimaryDisplay().workArea;
    let rect = {
        width:370,
        height:270,
        x:screenInfo.width-370,
        y:screenInfo.height,
    }
    try {
        msgWindow = new BrowserWindow({
            minWidth:370,
            minHeight:270,
            x: rect.x,
            y: rect.y,
            width: rect.width,
            height: rect.height,
            show:false,

            //下面4行实现无边框
            resizable: false,
            skipTaskbar:true,
            autoHideMenuBar:true,
            frame: false,

            // transparent:true,
            // titleBarOverlay: false,
            webPreferences: {
                nodeIntegration: true,
                contextIsolation:false,
                // webSecurity: false,
                // nodeIntegrationInWorker: true,
                preload: path.join(__dirname, 'preload.js'),
            }
        })
        msgWindow.setMenu(null);
        // msgWindow.setAlwaysOnTop(true, "floating", 1);
        // mainWindow.loadURL(path.join(__dirname, "/msgView","index.html"));
        await msgWindow.loadFile(path.join(__dirname, "/../src/msgView","index.html"));
        // if (NODE_ENV === "development") {
        //     msgWindow.webContents.openDevTools();
        // }
    } catch (error) {
        console.log(error);
    }
    msgWindow.on('closed', () => {
        msgWindow = null;
    });
}

ipcMain.on('showMsgWindow',(e,msgWinObj)=> {
    showMsgWindow(msgWinObj);

})

ipcMain.on('hideMsgWindow',()=>{
    hideMsgWindow();
})

function showMsgWindow(msgWinObj){
    // clearTimeout(msgWindowHideTimer);
    clearInterval(msgWindowHideAnimationTimer);
    // clearInterval(msgWindowShowAnimationTimer);
    if(msgWindowDisplay)return;
    msgWindowDisplay = true;


    let screenInfo = screen.getPrimaryDisplay().workArea;
    if (msgWindow === null)
        msgWindow = BrowserWindow.fromId(2);

    msgWindow.webContents.send('setMsgContent', msgWinObj.msg);
    if(msgWinObj.size)
        setMsgWindowSize(msgWinObj.size);
    msgWindow.show();
    //获取当前高度
    let tempY = msgWindow.getPosition()[1];
    let endY = screenInfo.height-msgWindow.getSize()[1];

    //弹出时候的动画效果
    msgWindowShowAnimationTimer = setInterval(()=>{
        if(tempY<=endY){
            // console.log("msgWindowShowAnimationTimer");
            clearInterval(msgWindowShowAnimationTimer);
        }
        tempY = lerp(tempY,endY,0.1);
        msgWindow.setBounds({ y: Math.floor(tempY) });
    },10);

    if(msgWinObj.timeout && msgWinObj.timeout !== 0){
        msgWindowHideTimer = setTimeout(()=>{
            clearInterval(msgWindowShowAnimationTimer);
            hideMsgWindow();
        },msgWinObj.timeout * 1000);
    }
}

function hideMsgWindow(){
    msgWindowDisplay = false;
    let screenInfo = screen.getPrimaryDisplay().workArea;
    let tempY = msgWindow.getPosition()[1];
    let endY = screenInfo.height+5;
    //弹出时候的动画效果
    msgWindowHideAnimationTimer = setInterval(()=>{
        if(tempY>=endY){
            // console.log("msgWindowHideAnimationTimer");
            msgWindow.hide();
            clearInterval(msgWindowHideAnimationTimer);
        }
        tempY = lerp(tempY,endY,0.1);
        msgWindow.setBounds({ y: Math.floor(tempY) });
    },10);
}

function setMsgWindowSize(size){
    let screenInfo = screen.getPrimaryDisplay().workArea;

    msgWindow.setBounds({
        x:screenInfo.width-size.width,
        y:screenInfo.height,
        height: size.height,
        width: size.width
    });
}

function lerp(a, b, p)
{
    if (Math.abs(a - b) < 0.1) return b;
    return a + (b - a) * p;
}
